import os
import sys
import warnings

import pandas as pd
import numpy as np

sys.path.append('..')
warnings.filterwarnings('ignore')

from tools.setting import DATA_DIR
from tools.tools import get_all_data, prices_cleaning, return_cleaning, ico_date, segmented_maxdrawdown_all, get_analysis_coin_list
from tools.time_tool import date_shifter
from tools.logger import logger

ANALYSIS_DATA_DIR = os.path.join(DATA_DIR, 'analysis')
os.makedirs(ANALYSIS_DATA_DIR, exist_ok=True)
WEEKS_ANALYSIS_DATA_DIR = os.path.join(ANALYSIS_DATA_DIR, 'weeks')
os.makedirs(WEEKS_ANALYSIS_DATA_DIR, exist_ok=True)


def analysis_weeks():
    # 读取数据
    logger.info('读取数据')
    df_price, df_amount, df_marketcap = get_all_data()
    df_price = df_price.apply(prices_cleaning)

    file_path = os.path.join(DATA_DIR, 'amount_data')
    file_name = os.path.join(file_path, f'total_marketcap_and_amount.csv')
    total_amount = pd.read_csv(file_name)
    total_amount.drop_duplicates(subset=['end_date'], inplace=True)
    total_amount.set_index('end_date', inplace=True)
    total_amount_series = total_amount['amount']
    total_amount_series = total_amount_series.rolling(30).mean()
    amount_threshold = total_amount_series / 100000
    amount_threshold[amount_threshold < 20000] = 20000
    kick_out_date = pd.date_range('2017-09-01', '2017-10-01', freq='D').tolist()
    kick_out_date = [x.strftime("%Y-%m-%d") for x in kick_out_date]
    amount_threshold[kick_out_date] = 0
    amount_threshold = amount_threshold[df_price.index]

    start_date = '2015-01-01'

    # 计算ICO日期
    logger.info('计算ICO日期')
    ico_date_series = df_price.apply(ico_date).T[0]

    # 剔除不需要的代币（主要是稳定币）
    all_coins_name_without_stablecoins = get_analysis_coin_list(df_price)
    df_price = df_price[all_coins_name_without_stablecoins]
    df_amount = df_amount[all_coins_name_without_stablecoins]
    df_marketcap = df_marketcap[all_coins_name_without_stablecoins]


    ### 预处理各种因子
    logger.info('预处理各种因子')
    # 计算各个币的均线上穿情况
    all_bias300_df = df_price - df_price.rolling(300).mean()
    all_bias200_df = df_price - df_price.rolling(200).mean()
    all_bias155_df = df_price - df_price.rolling(155).mean()
    all_bias120_df = df_price - df_price.rolling(120).mean()
    all_bias60_df = df_price - df_price.rolling(60).mean()
    all_bias30_df = df_price - df_price.rolling(30).mean()

    all_osc300_df = (df_price - df_price.rolling(300).mean()) / df_price.rolling(300).mean()
    all_osc200_df = (df_price - df_price.rolling(200).mean()) / df_price.rolling(200).mean()
    all_osc155_df = (df_price - df_price.rolling(155).mean()) / df_price.rolling(155).mean()
    all_osc120_df = (df_price - df_price.rolling(120).mean()) / df_price.rolling(120).mean()
    all_osc60_df = (df_price - df_price.rolling(60).mean()) / df_price.rolling(60).mean()
    all_osc30_df = (df_price - df_price.rolling(30).mean()) / df_price.rolling(30).mean()

    # 计算近1, 3 , 4, 6月涨跌幅
    all_3monthly_ret_df = df_price.pct_change(90)
    all_3monthly_ret_df = all_3monthly_ret_df.replace([float('inf'), float('-inf')], [np.nan, np.nan])  # 剔除异常涨跌幅
    all_3monthly_ret_df[all_3monthly_ret_df > 1000000] = np.nan
    all_4monthly_ret_df = df_price.pct_change(120)
    all_4monthly_ret_df = all_4monthly_ret_df.replace([float('inf'), float('-inf')], [np.nan, np.nan])  # 剔除异常涨跌幅
    all_4monthly_ret_df[all_4monthly_ret_df > 1000000] = np.nan
    all_6monthly_ret_df = df_price.pct_change(180)
    all_6monthly_ret_df = all_6monthly_ret_df.replace([float('inf'), float('-inf')], [np.nan, np.nan])  # 剔除异常涨跌幅
    all_6monthly_ret_df[all_6monthly_ret_df > 1000000] = np.nan

    # 创近1,3,6个月新高或新低
    all_high30_df = (df_price - df_price.rolling(30).max())
    all_high60_df = (df_price - df_price.rolling(60).max())
    all_high90_df = (df_price - df_price.rolling(90).max())
    all_high120_df = (df_price - df_price.rolling(120).max())
    all_high180_df = (df_price - df_price.rolling(180).max())
    all_low30_df = (df_price.rolling(30).min() - df_price)
    all_low60_df = (df_price.rolling(60).min() - df_price)
    all_low90_df = (df_price.rolling(90).min() - df_price)
    all_low120_df = (df_price.rolling(129).min() - df_price)
    all_low180_df = (df_price.rolling(180).min() - df_price)

    # 距离,1周，近1，3,4，6个月高点回撤，低点上涨幅度
    all_high7ret_df = (df_price - df_price.rolling(7).max()) / df_price.rolling(7).max()
    all_high30ret_df = (df_price - df_price.rolling(30).max()) / df_price.rolling(30).max()
    all_high90ret_df = (df_price - df_price.rolling(90).max()) / df_price.rolling(90).max()
    all_high120ret_df = (df_price - df_price.rolling(120).max()) / df_price.rolling(120).max()
    all_high180ret_df = (df_price - df_price.rolling(180).max()) / df_price.rolling(180).max()
    all_low7ret_df = (df_price - df_price.rolling(7).min()) / df_price.rolling(7).min()
    all_low30ret_df = (df_price - df_price.rolling(30).min()) / df_price.rolling(30).min()
    all_low90ret_df = (df_price - df_price.rolling(90).min()) / df_price.rolling(90).min()
    all_low120ret_df = (df_price - df_price.rolling(120).min()) / df_price.rolling(120).min()
    all_low180ret_df = (df_price - df_price.rolling(180).min()) / df_price.rolling(180).min()

    # 选取需要统计的时间段
    logger.info('选取需要统计的时间段')
    coins_num_eachday = df_price.count(axis=1)
    all_days = list(coins_num_eachday.index)
    all_days.sort(reverse=True)
    analysis_end_date = all_days[0]
    for day in all_days:
        if coins_num_eachday[day] > 8000:
            analysis_end_date = day
            break
        else:
            continue
    df_price = df_price.loc[start_date:analysis_end_date, ]
    df_amount = df_amount.loc[start_date:analysis_end_date, ]
    df_marketcap = df_marketcap.loc[start_date:analysis_end_date, ]
    total_amount_series = total_amount_series[start_date:analysis_end_date]
    amount_threshold = amount_threshold[start_date:analysis_end_date]

    # 获取或计算回撤数据
    logger.info('获取或计算回撤数据')
    drawdown_file_name = 'E:\\data\\analysis\\drawdown\\price_drawdown.csv'
    if not os.path.exists(drawdown_file_name):
        df_price_drawdown = segmented_maxdrawdown_all(df_price, ['2020-03-12'])
    else:
        df_price_drawdown = pd.read_csv(drawdown_file_name, index_col='end_date')

    # 计算处理日收益率、周收益率、月收益率，用于后续统计
    logger.info('计算日收益率、周收益率、月收益率')
    df_daily_return = df_price.pct_change()
    df_daily_return = df_daily_return.replace([float('inf'), float('-inf')], [np.nan, np.nan])  # 剔除异常涨跌幅
    df_daily_return[df_daily_return > 100] = np.nan
    df_weekly_return = df_price.pct_change(7)
    df_weekly_return = df_weekly_return.replace([float('inf'), float('-inf')], [np.nan, np.nan])  # 剔除异常涨跌幅
    df_weekly_return[df_weekly_return > 1000] = np.nan
    df_monthly_return = df_price.pct_change(30)
    df_monthly_return = df_monthly_return.replace([float('inf'), float('-inf')], [np.nan, np.nan])  # 剔除异常涨跌幅
    df_monthly_return[df_monthly_return > 10000] = np.nan

    # 统计需要的因子、比例或者分布，并保存。
    logger.info('按类别统计需要的信息')
    for ret_type, ret in enumerate([df_daily_return, df_weekly_return, df_monthly_return]):
        if ret_type == 0:
            ret_type = 'daily'
            ret_gap = [float('-inf'), -1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.15, -0.1, -0.05, 0, 0.05,
                       0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 5, 10, float('inf')]
        elif ret_type == 1:
            ret_type = 'weekly'
            ret_gap = [float('-inf'), -1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.15, -0.1, -0.05, 0, 0.05,
                       0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 5, 10, float('inf')]
        elif ret_type == 2:
            ret_type = 'monthly'
            ret_gap = [float('-inf'), -1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.15, -0.1, -0.05, 0, 0.05,
                       0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 5, 10, float('inf')]
        else:
            raise NotImplementedError

        ret_gap_list = [[ret_gap[i], ret_gap[i + 1]] for i in range(len(ret_gap) - 1)]
        logger.info(f'计算{ret_type}类因子')
        all_ret_df = ret.dropna(how='all', axis=0)  # 剔除全为空的行, 计算收益率导致的前几行为空
        start_update_date = all_ret_df.index[0]
        result_file_name = os.path.join(WEEKS_ANALYSIS_DATA_DIR, f'All Coins {ret_type} Return Analysis.csv')
        if os.path.exists(result_file_name):
            all_result_df = pd.read_csv(result_file_name, index_col='end_date')
            start_update_date = date_shifter(all_result_df.index[-1], step='days', how_many=-14)
            all_result_df = pd.DataFrame(all_result_df, index=all_ret_df.index)
            for kick_small_amount in [True, False]:  # 这几列数据需要填充为list， 需要提前设置dtype为object
                for topcap_num in ['all', 'top100', 'top200']:
                    all_result_df[f'{topcap_num}_all_cleaned_coins_name_{kick_small_amount}'] = [
                        eval(i) if isinstance(i, str) else i for i in
                        all_result_df[f'{topcap_num}_all_cleaned_coins_name_{kick_small_amount}']]
        else:
            all_result_df = pd.DataFrame(index=all_ret_df.index)
            for kick_small_amount in [True, False]:  # 这几列数据需要填充为list， 需要提前设置dtype为object
                for topcap_num in ['all', 'top100', 'top200']:
                    all_result_df[f'{topcap_num}_all_cleaned_coins_name_{kick_small_amount}'] = np.nan
                    all_result_df[f'{topcap_num}_all_cleaned_coins_name_{kick_small_amount}'] = all_result_df[
                        f'{topcap_num}_all_cleaned_coins_name_{kick_small_amount}'].astype('object')

        coin_name_file_name = os.path.join(WEEKS_ANALYSIS_DATA_DIR, f'All Coins {ret_type} Name.csv')
        if os.path.exists(coin_name_file_name):
            all_coins_name_df = pd.read_csv(coin_name_file_name, index_col='end_date')
            all_coins_name_df = pd.DataFrame(all_coins_name_df, index=all_ret_df.index)
        else:
            all_coins_name_df = pd.DataFrame(index=all_ret_df.index)
        earliest_day = all_ret_df.index[0]

        # 统计整体的收益率分布情况, 统计剔除小的交易量
        logger.info('统计整体的收益率分布情况')
        for kick_small_amount in [True, False]:
            if kick_small_amount:
                small_amount_series = total_amount_series / 100000  # 最小交易额限制为当天全市场交易额除以100000
                small_amount_series[small_amount_series < 20000] = 20000  # 计算出来最小交易额阈值小于20000的统统设置为20000
                temp_amount = df_amount.sub(small_amount_series, axis=0)
                temp_amount = temp_amount.loc[all_ret_df.index]
                temp_ret_df = all_ret_df[temp_amount >= 0]  # 所有满足最小交易额的收益率数据
            else:
                temp_ret_df = all_ret_df[df_amount >= 0]  # 如果无需剔除最小交易额，则只要求交易额数据大于等于0，其实就是要求不为空

            # 对BTC收益率进行统计
            btc_ret_series = temp_ret_df['bitcoin']
            btc_ret_series.dropna(inplace=True)
            btc_ret_describe = btc_ret_series.describe()
            btc_ret_describe['median'] = btc_ret_series.median()
            file_name = os.path.join(WEEKS_ANALYSIS_DATA_DIR,
                                     f'BTC_{ret_type}_return_describe_kicksmalltrade_{kick_small_amount}.csv')
            btc_ret_describe.to_csv(file_name)

            btc_ret_series.name = 'ret'
            btc_ret_df = pd.DataFrame(btc_ret_series)
            btc_ret_df['name'] = 'bitcoin'
            btc_ret_df.sort_values(by=['ret'], inplace=True, ascending=False)
            file_name = os.path.join(WEEKS_ANALYSIS_DATA_DIR,
                                     f'BTC_{ret_type}_return_series_kicksmalltrade_{kick_small_amount}.csv')
            btc_ret_df.to_csv(file_name)

            # 所有收益率拼成一个series，好对所有收益率进行画图和统计
            all_ret_series_list = []
            for i in all_coins_name_without_stablecoins:
                temp_ret_series = temp_ret_df[i]
                temp_ret_series.dropna(inplace=True)
                temp_ret_series.name = 'ret'
                temp_temp_ret_df = pd.DataFrame(temp_ret_series)
                temp_temp_ret_df['name'] = i
                all_ret_series_list.append(temp_temp_ret_df)
            temp_all_ret_df = pd.concat(all_ret_series_list, axis=0)
            all_ret_series = temp_all_ret_df['ret']
            all_ret_describe = all_ret_series.describe()
            all_ret_describe['median'] = all_ret_series.median()
            file_name = os.path.join(WEEKS_ANALYSIS_DATA_DIR,
                                     f'all_{ret_type}_return_describe_kicksmalltrade_{kick_small_amount}.csv')
            all_ret_describe.to_csv(file_name)

            temp_all_ret_df.sort_values(by=['ret'], inplace=True, ascending=False)
            file_name = os.path.join(WEEKS_ANALYSIS_DATA_DIR,
                                     f'all_{ret_type}_return_series_kicksmalltrade_{kick_small_amount}.csv')
            temp_all_ret_df.to_csv(file_name)
            del all_ret_series
            del temp_all_ret_df

        logger.info('按成交额筛选收益率')
        coin_list = list(ret.columns)
        results = []
        for coin in coin_list:
            results.append(return_cleaning(coin, ico_date_series[coin], ret[coin], df_amount[coin], amount_threshold))
            if len(results) % 1000 == 0:
                logger.info(len(results) / len(coin_list) * 100)
        df_cleaned_return = pd.concat(results, axis=1)
        file_name = os.path.join(WEEKS_ANALYSIS_DATA_DIR, f'all_{ret_type}_return_cleaned.csv')
        df_cleaned_return.to_csv(file_name)

        df_cleaned_return = df_cleaned_return[start_date:]

        for date in df_cleaned_return[start_update_date:].index:
            logger.info(f'开始计算{date}的数据')

            # 当天有价格数据的币的名称
            temp_price_series = df_price.loc[date, :]
            temp_price_series = temp_price_series.dropna()
            all_coins_name_df.loc[date, f'all_coins_name'] = f'{list(temp_price_series.index)}'  # 所有符合条件的币的名字
            # 当天每个币的交易额情况
            amount_series = df_amount.loc[date, all_coins_name_without_stablecoins]
            amount_series = amount_series.replace(0, np.nan)  # 交易额为0的需要剔除

            # 剔除交易额过小的情况
            kick_small_amount = True

            small_amount_threshold_this_date = total_amount_series[date] / 100000
            if small_amount_threshold_this_date < 20000:
                small_amount_threshold_this_date = 20000
            temp_amount_series = amount_series[
                (amount_series >= small_amount_threshold_this_date)]  # 剔除交易额小于多少的币 todo 交易额为空的也剔除掉

            if temp_amount_series.empty:  # 当天没有交易额超过small_amount_threshold_this_date的币， 跳过该交易日
                continue

            temp_ret_series = df_cleaned_return.loc[date, temp_amount_series.index]  # 剔除交易额大于多少的币的这一天收益率
            temp_cap_series = df_marketcap.loc[date, temp_amount_series.index]  # 剔除交易额大于多少的币的这一天市值
            temp_cap_series = temp_cap_series.replace(0, np.nan)  # 市值为0的需要剔除
            all_coins_this_date = list(set(temp_ret_series.dropna().index).intersection(
                set(temp_cap_series.dropna().index)))  # todo 当天交易额满足要求，且收益率数据和市值数据不为空
            temp_amount_series = temp_amount_series[all_coins_this_date]
            if temp_amount_series.empty:  # 同时满足三个条件的一个币都没有，跳过该交易日
                continue
            temp_ret_series = temp_ret_series[all_coins_this_date]
            temp_cap_series = temp_cap_series[all_coins_this_date]
            temp_ret_series.sort_values(ascending=False, inplace=True)  # 收益率从大到小排序

            # 币走势一致性（可以分析以下两个指标 1、通过PCA第一主成分贡献率；2、或者通过两两相关性大于某一阈值占比）时间窗口测试30， 60， 90， 120， 180， 365
            for previous_day in [10, 20, 30, 60, 90, 180, 365]:
                days_ago = date_shifter(date, step='days', how_many=-previous_day)
                if days_ago < earliest_day:  # 数据长度不够，跳过
                    pass
                else:
                    # 两两相关性大于某一阈值占比
                    temp_ret_df = all_ret_df.loc[days_ago: date, all_coins_this_date]
                    corr_df = temp_ret_df.corr(method='pearson', min_periods=10)
                    corr_up_df = pd.DataFrame(np.triu(corr_df.values, 1))
                    for corr_thereshold in [0.5, 0.6, 0.7, 0.8]:
                        big_corr_ratio = sum((corr_up_df >= corr_thereshold).sum()) / (
                                    (np.square(corr_up_df.shape[0]) - corr_up_df.shape[0]) / 2)
                        all_result_df.loc[date, f'{previous_day}days_corr>{corr_thereshold}_ratio'] = big_corr_ratio

            # 币横截面走势分化度
            all_result_df.loc[date, f'whole_market_ret_divergence_{kick_small_amount}'] = np.std(temp_ret_series)

            # 统计所有符合条件的币，统计市值前100，前200的币的情况
            for topcap_num in ['all']:
                temp_cap_series_for_this_test = temp_cap_series.replace(0, np.nan)  # 剔除市值为0的
                temp_cap_series_for_this_test.dropna(inplace=True)  # 剔除市值为空的
                temp_cap_series_for_this_test.sort_values(ascending=False, inplace=True)  # 市值从大到小排列
                if topcap_num == 'all':
                    all_coins_for_cal = temp_amount_series.index
                    temp_cap_series_new = temp_cap_series.copy()
                    temp_ret_series_new = temp_ret_series.copy()
                    temp_amount_series_new = temp_amount_series.copy()
                elif topcap_num == 'top100':
                    all_coins_for_cal = temp_cap_series_for_this_test.index[:100]
                    temp_cap_series_new = temp_cap_series[all_coins_for_cal]
                    temp_ret_series_new = temp_ret_series[all_coins_for_cal]
                    temp_amount_series_new = temp_amount_series[all_coins_for_cal]
                elif topcap_num == 'top200':
                    all_coins_for_cal = temp_cap_series_for_this_test.index[:200]
                    temp_cap_series_new = temp_cap_series[all_coins_for_cal]
                    temp_ret_series_new = temp_ret_series[all_coins_for_cal]
                    temp_amount_series_new = temp_amount_series[all_coins_for_cal]

                # 阈值设定
                osc_ret_gap = [float('-inf'), -1, -0.8, -0.6, -0.4, -0.3, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15,
                               0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 1, 1.5, 2, 3, 5, 10, float('inf')]
                positive_ret_gap = [0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 1, 1.5, 2, 3, 5, 10, float('inf')]
                negative_ret_gap = [float('-inf'), -1, -0.8, -0.6, -0.5, -0.4, -0.3, -0.2, -0.15, -0.1, -0.05, 0]
                osc_ret_gap_list = [[osc_ret_gap[i], osc_ret_gap[i + 1]] for i in range(len(osc_ret_gap) - 1)]
                positive_ret_gap_list = [[positive_ret_gap[i], positive_ret_gap[i + 1]] for i in
                                         range(len(positive_ret_gap) - 1)]
                negative_ret_gap_list = [[negative_ret_gap[i], negative_ret_gap[i + 1]] for i in
                                         range(len(negative_ret_gap) - 1)]

                # 统计上传下穿均线的情况
                for ma_period in [30, 60, 120, 155, 200, 300]:
                    if ma_period == 30:
                        temp_bias_df = all_bias30_df
                        temp_osc_df = all_osc30_df
                    elif ma_period == 60:
                        temp_bias_df = all_bias60_df
                        temp_osc_df = all_osc60_df
                    elif ma_period == 120:
                        temp_bias_df = all_bias120_df
                        temp_osc_df = all_osc120_df
                    elif ma_period == 155:
                        temp_bias_df = all_bias155_df
                        temp_osc_df = all_osc155_df
                    elif ma_period == 200:
                        temp_bias_df = all_bias200_df
                        temp_osc_df = all_osc200_df
                    elif ma_period == 300:
                        temp_bias_df = all_bias300_df
                        temp_osc_df = all_osc300_df
                    else:
                        raise NotImplementedError

                    bias_series = temp_bias_df.loc[date, all_coins_for_cal]
                    bias_series.dropna(inplace=True)
                    all_result_df.loc[date, f'{topcap_num}_bias{ma_period}_num_smalltrade_{kick_small_amount}'] = len(
                        bias_series[bias_series > 0])
                    if len(bias_series) == 0:
                        all_result_df.loc[
                            date, f'{topcap_num}_bias{ma_period}_num_ratio_smalltrade_{kick_small_amount}'] = np.nan
                    else:
                        all_result_df.loc[
                            date, f'{topcap_num}_bias{ma_period}_num_ratio_smalltrade_{kick_small_amount}'] = len(
                            bias_series[bias_series > 0]) / len(bias_series)

                    # 统计每天osc分布情况（根据不同的收益率区间划分）
                    osc_series = temp_osc_df.loc[date, all_coins_for_cal]
                    osc_series.dropna(inplace=True)
                    for gap in osc_ret_gap_list:
                        temp_osc_series = osc_series[(osc_series > gap[0]) & (osc_series <= gap[1])]
                        all_result_df.loc[
                            date, f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}'] = len(
                            temp_osc_series)
                    gap_columns_name = [f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}' for gap in
                                        osc_ret_gap_list]
                    all_result_df.loc[date, gap_columns_name] = all_result_df.loc[date, gap_columns_name] / \
                                                                all_result_df.loc[date, gap_columns_name].sum()

                # 近1,3,4,6月绝对收益率分布情况
                if ret_type == 'daily':
                    for highlow_period in [30, 90, 120, 180]:
                        if highlow_period == 30:
                            temp_monthly_ret_df = df_monthly_return
                        elif highlow_period == 90:
                            temp_monthly_ret_df = all_3monthly_ret_df
                        elif highlow_period == 120:
                            temp_monthly_ret_df = all_4monthly_ret_df
                        elif highlow_period == 180:
                            temp_monthly_ret_df = all_6monthly_ret_df
                        else:
                            raise NotImplementedError

                        monthly_ret_series = temp_monthly_ret_df.loc[date, all_coins_for_cal]
                        monthly_ret_series.dropna(inplace=True)
                        for gap in osc_ret_gap_list:
                            temp_monthly_ret_series = monthly_ret_series[
                                (monthly_ret_series > gap[0]) & (monthly_ret_series <= gap[1])]
                            all_result_df.loc[
                                date, f'{topcap_num}_monthlyret{highlow_period}_{gap}_smalltrade_{kick_small_amount}'] = len(
                                temp_monthly_ret_series)
                        gap_columns_name = [
                            f'{topcap_num}_monthlyret{highlow_period}_{gap}_smalltrade_{kick_small_amount}' for gap in
                            osc_ret_gap_list]
                        all_result_df.loc[date, gap_columns_name] = all_result_df.loc[date, gap_columns_name] / \
                                                                    all_result_df.loc[date, gap_columns_name].sum()

                # 自1, 3, 4, 6月高点回撤，低点上涨幅度分布
                if ret_type == 'daily':
                    for highlow_period in [7, 30, 90, 120, 180]:
                        if highlow_period == 7:
                            temp_highret_df = all_high7ret_df
                            temp_lowret_df = all_low7ret_df
                        elif highlow_period == 30:
                            temp_highret_df = all_high30ret_df
                            temp_lowret_df = all_low30ret_df
                        elif highlow_period == 90:
                            temp_highret_df = all_high90ret_df
                            temp_lowret_df = all_low90ret_df
                        elif highlow_period == 120:
                            temp_highret_df = all_high120ret_df
                            temp_lowret_df = all_low120ret_df
                        elif highlow_period == 180:
                            temp_highret_df = all_high180ret_df
                            temp_lowret_df = all_low180ret_df
                        else:
                            raise NotImplementedError

                        # 统计高点回撤收益率分布
                        highret_series = temp_highret_df.loc[date, all_coins_for_cal]
                        highret_series.dropna(inplace=True)
                        for gap in negative_ret_gap_list:
                            temp_highret_series = highret_series[(highret_series > gap[0]) & (highret_series <= gap[1])]
                            all_result_df.loc[
                                date, f'{topcap_num}_highret{highlow_period}_{gap}_smalltrade_{kick_small_amount}'] = len(
                                temp_highret_series)
                        gap_columns_name = [f'{topcap_num}_highret{highlow_period}_{gap}_smalltrade_{kick_small_amount}'
                                            for gap in negative_ret_gap_list]
                        all_result_df.loc[date, gap_columns_name] = all_result_df.loc[date, gap_columns_name] / \
                                                                    all_result_df.loc[date, gap_columns_name].sum()

                        # 统计低点回升收益率分布
                        lowret_series = temp_lowret_df.loc[date, all_coins_for_cal]
                        lowret_series.dropna(inplace=True)
                        for gap in positive_ret_gap_list:
                            temp_lowret_series = lowret_series[(lowret_series > gap[0]) & (lowret_series <= gap[1])]
                            all_result_df.loc[
                                date, f'{topcap_num}_lowret{highlow_period}_{gap}_smalltrade_{kick_small_amount}'] = len(
                                temp_lowret_series)
                        gap_columns_name = [f'{topcap_num}_lowret{highlow_period}_{gap}_smalltrade_{kick_small_amount}'
                                            for gap in positive_ret_gap_list]
                        all_result_df.loc[date, gap_columns_name] = all_result_df.loc[date, gap_columns_name] / \
                                                                    all_result_df.loc[date, gap_columns_name].sum()

                # 创1, 2, 3, 4, 6月新高新低占比分布
                if ret_type == 'daily':
                    for highlow_period in [30, 60, 90, 120, 180]:
                        if highlow_period == 30:
                            temp_high_df = all_high30_df
                            temp_low_df = all_low30_df
                        elif highlow_period == 60:
                            temp_high_df = all_high60_df
                            temp_low_df = all_low60_df
                        elif highlow_period == 90:
                            temp_high_df = all_high90_df
                            temp_low_df = all_low90_df
                        elif highlow_period == 120:
                            temp_high_df = all_high120_df
                            temp_low_df = all_low120_df
                        elif highlow_period == 180:
                            temp_high_df = all_high180_df
                            temp_low_df = all_low180_df
                        else:
                            raise NotImplementedError

                        low_series = temp_low_df.loc[date, all_coins_for_cal]
                        low_series.dropna(inplace=True)
                        temp_low_series = low_series[low_series == 0]
                        all_result_df.loc[
                            date, f'{topcap_num}_low{highlow_period}_num_ratio_smalltrade_{kick_small_amount}'] = len(
                            temp_low_series) / len(low_series)

                        high_series = temp_high_df.loc[date, all_coins_for_cal]
                        high_series.dropna(inplace=True)
                        temp_high_series = high_series[high_series == 0]
                        all_result_df.loc[
                            date, f'{topcap_num}_high{highlow_period}_num_ratio_smalltrade_{kick_small_amount}'] = len(
                            temp_high_series) / len(high_series)

                # 比BTC近1天，近一周，近1,3,6月收益率高的币占比，或低的币占比
                if ret_type == 'daily':
                    for compare_period in ['daily', 'weekly', '1monthly', '3monthly', '6monthly']:
                        if compare_period == 'daily':
                            temp_compare_ret_series = df_daily_return.loc[date, all_coins_for_cal]
                            threshold_list = [0, 0.001, 0.005, 0.01, 0.03, 0.05, 0.1]
                        elif compare_period == 'weekly':
                            temp_compare_ret_series = df_weekly_return.loc[date, all_coins_for_cal]
                            threshold_list = [0, 0.01, 0.03, 0.05, 0.1, 0.15, 0.2]
                        elif compare_period == '1monthly':
                            temp_compare_ret_series = df_monthly_return.loc[date, all_coins_for_cal]
                            threshold_list = [0, 0.05, 0.1, 0.15, 0.2, 0.3]
                        elif compare_period == '3monthly':
                            temp_compare_ret_series = all_3monthly_ret_df.loc[date, all_coins_for_cal]
                            threshold_list = [0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.5]
                        elif compare_period == '6monthly':
                            temp_compare_ret_series = all_6monthly_ret_df.loc[date, all_coins_for_cal]
                            threshold_list = [0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.5]
                        else:
                            raise NotImplementedError
                        temp_compare_ret_series.dropna(inplace=True)
                        if len(temp_compare_ret_series) == 0:
                            continue
                        else:
                            for threshold in threshold_list:
                                bigger_ret_series = temp_compare_ret_series[
                                    temp_compare_ret_series > (temp_compare_ret_series['bitcoin'] + threshold)]
                                all_result_df.loc[
                                    date, f'{topcap_num}_{compare_period}_ret_biggerthan_btc_{threshold}_ratio_smalltrade_{kick_small_amount}'] = len(
                                    bigger_ret_series) / len(temp_compare_ret_series)
                            for threshold in threshold_list:
                                smaller_ret_series = temp_compare_ret_series[
                                    temp_compare_ret_series < (temp_compare_ret_series['bitcoin'] - threshold)]
                                all_result_df.loc[
                                    date, f'{topcap_num}_{compare_period}_ret_smallerthan_btc_{-threshold}_ratio_smalltrade_{kick_small_amount}'] = len(
                                    smaller_ret_series) / len(temp_compare_ret_series)

                # 比BTC近一周，近1,3,6月高点回撤多的币占比，或者低点上涨多的币占比
                if ret_type == 'daily':
                    for compare_period in [7, 30, 90, 120, 180]:
                        if compare_period == 7:
                            temp_highret_compare_ret_series = all_high7ret_df.loc[date, all_coins_for_cal]
                            temp_lowret_compare_ret_series = all_low7ret_df.loc[date, all_coins_for_cal]
                            highret_threshold_list = [0.01, 0.03, 0.05, 0.1, 0.15, 0.2]
                            lowret_threshold_list = [0.01, 0.03, 0.05, 0.1, 0.15, 0.2]
                        elif compare_period == 30:
                            temp_highret_compare_ret_series = all_high30ret_df.loc[date, all_coins_for_cal]
                            temp_lowret_compare_ret_series = all_low30ret_df.loc[date, all_coins_for_cal]
                            highret_threshold_list = [0.03, 0.05, 0.1, 0.15, 0.2, 0.3]
                            lowret_threshold_list = [0.03, 0.05, 0.1, 0.15, 0.2, 0.3]
                        elif compare_period == 90:
                            temp_highret_compare_ret_series = all_high90ret_df.loc[date, all_coins_for_cal]
                            temp_lowret_compare_ret_series = all_low90ret_df.loc[date, all_coins_for_cal]
                            highret_threshold_list = [0.03, 0.05, 0.1, 0.15, 0.2, 0.3]
                            lowret_threshold_list = [0.05, 0.1, 0.15, 0.2, 0.3, 0.5]
                        elif compare_period == 120:
                            temp_highret_compare_ret_series = all_high120ret_df.loc[date, all_coins_for_cal]
                            temp_lowret_compare_ret_series = all_low120ret_df.loc[date, all_coins_for_cal]
                            highret_threshold_list = [0.03, 0.05, 0.1, 0.15, 0.2, 0.3]
                            lowret_threshold_list = [0.05, 0.1, 0.15, 0.2, 0.3, 0.5]
                        elif compare_period == 180:
                            temp_highret_compare_ret_series = all_high180ret_df.loc[date, all_coins_for_cal]
                            temp_lowret_compare_ret_series = all_low180ret_df.loc[date, all_coins_for_cal]
                            highret_threshold_list = [0.03, 0.05, 0.1, 0.15, 0.2, 0.3]
                            lowret_threshold_list = [0.05, 0.1, 0.15, 0.2, 0.3, 0.5]
                        else:
                            raise NotImplementedError

                        temp_highret_compare_ret_series.dropna(inplace=True)
                        if len(temp_highret_compare_ret_series) != 0:
                            for threshold in highret_threshold_list:
                                smaller_ret_series = temp_highret_compare_ret_series[temp_highret_compare_ret_series < (
                                            temp_highret_compare_ret_series['bitcoin'] - threshold)]
                                all_result_df.loc[
                                    date, f'{topcap_num}_{compare_period}_highret_smallerthan_btc_{-threshold}_ratio_smalltrade_{kick_small_amount}'] = len(
                                    smaller_ret_series) / len(temp_highret_compare_ret_series)
                        temp_lowret_compare_ret_series.dropna(inplace=True)
                        if len(temp_lowret_compare_ret_series) != 0:
                            for threshold in lowret_threshold_list:
                                bigger_ret_series = temp_lowret_compare_ret_series[temp_lowret_compare_ret_series > (
                                            temp_lowret_compare_ret_series['bitcoin'] + threshold)]
                                all_result_df.loc[
                                    date, f'{topcap_num}_{compare_period}_lowret_biggerthan_btc_{threshold}_ratio_smalltrade_{kick_small_amount}'] = len(
                                    bigger_ret_series) / len(temp_lowret_compare_ret_series)

                # 统计每天收益率分布情况（根据不同的收益率区间划分）
                for gap in ret_gap_list:
                    temp_gap_ret_series = temp_ret_series_new[
                        (temp_ret_series_new > gap[0]) & (temp_ret_series_new <= gap[1])]
                    all_result_df.loc[date, f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}'] = len(
                        temp_gap_ret_series)
                gap_columns_name = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}' for gap in ret_gap_list]
                all_result_df.loc[date, f'{topcap_num}_all_coins_num_{kick_small_amount}'] = all_result_df.loc[
                    date, gap_columns_name].sum()
                all_result_df.loc[date, gap_columns_name] = all_result_df.loc[date, gap_columns_name] / \
                                                            all_result_df.loc[date, gap_columns_name].sum()

                # 所有符合条件的币的名字(清洗之后按市值从大到小排序)
                temp_cap_series_name = temp_cap_series_new.sort_values(ascending=False)  # 市值从大到小排列
                all_result_df.at[date, f'{topcap_num}_all_cleaned_coins_name_{kick_small_amount}'] = list(
                    temp_cap_series_name.index)

                temp_cap_series_new = temp_cap_series_new.replace(0, np.nan)  # 剔除市值为0的
                temp_cap_series_new.dropna(inplace=True)  # 剔除市值为空的
                temp_cap_series_new.sort_values(ascending=False, inplace=True)  # 市值从大到小排列
                temp_cap_series_new = temp_cap_series_new.astype(float)

                # 每天市值前n的币有多少是之前也在前n的
                for previous_day in [30, 90, 180]:
                    days_ago = date_shifter(date, step='days', how_many=-previous_day)
                    for top_num in [30, 50]:
                        if days_ago < earliest_day:
                            all_result_df.loc[
                                date, f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago'] = np.nan
                        else:
                            # 当前市值前n的币的名称
                            top_coin_this_date = set(temp_cap_series_new[:top_num].index)
                            # n天前市值前n的币的名称
                            top_coin_days_ago = all_result_df.loc[
                                                    days_ago, f'{topcap_num}_all_cleaned_coins_name_{kick_small_amount}'][
                                                :top_num]

                            # 每天市值前n的币有多少是n天前在市值前n的
                            all_result_df.loc[date, f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago'] = len(
                                top_coin_this_date.intersection(top_coin_days_ago))
                            all_result_df.loc[
                                date, f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago_ratio'] = len(
                                top_coin_this_date.intersection(top_coin_days_ago)) / len(top_coin_this_date)

                # 不同市值组别累计至今的累计收益率情况（累计1个月，3个月，6个月，12个月）
                if ret_type == 'daily':
                    previous_365_day = date_shifter(date, step='days', how_many=-365)
                    previous_180_day = date_shifter(date, step='days', how_many=-180)
                    previous_90_day = date_shifter(date, step='days', how_many=-90)
                    previous_30_day = date_shifter(date, step='days', how_many=-30)

                    for previous_day in [30, 90, 180, 365]:
                        if previous_day == 30:
                            days_ago = previous_30_day
                        elif previous_day == 90:
                            days_ago = previous_90_day
                        elif previous_day == 180:
                            days_ago = previous_180_day
                        elif previous_day == 365:
                            days_ago = previous_365_day
                        else:
                            raise NotImplementedError
                        for top_num in [1, 3, 5, 10, 50, 100]:
                            if days_ago < earliest_day:
                                all_result_df.loc[date, f'{topcap_num}_{top_num}_{previous_day}days_cumret'] = np.nan
                            else:
                                # n天前市值前n的币的名称
                                coins_days_ago = all_result_df.loc[
                                    days_ago, f'{topcap_num}_all_cleaned_coins_name_{kick_small_amount}']
                                if len(coins_days_ago) < top_num:
                                    all_result_df.loc[
                                        date, f'{topcap_num}_{top_num}_{previous_day}days_cumret'] = np.nan
                                else:
                                    if top_num == 1:
                                        top_coin_days_ago = coins_days_ago[0:1]
                                    elif top_num == 3:
                                        top_coin_days_ago = coins_days_ago[1:3]
                                    elif top_num == 5:
                                        top_coin_days_ago = coins_days_ago[3:5]
                                    elif top_num == 10:
                                        top_coin_days_ago = coins_days_ago[5:10]
                                    elif top_num == 50:
                                        top_coin_days_ago = coins_days_ago[10:50]
                                    elif top_num == 100:
                                        top_coin_days_ago = coins_days_ago[50:100]

                                    try:
                                        top_coin_ret_days_ago = all_ret_df.loc[days_ago:date, top_coin_days_ago].mean(axis=1)
                                    except KeyError:
                                        filter_top_coin_days_ago = []
                                        for i in top_coin_days_ago:
                                            if i in all_ret_df.columns:
                                                filter_top_coin_days_ago.append(i)
                                        top_coin_ret_days_ago = all_ret_df.loc[days_ago:date, filter_top_coin_days_ago].mean(axis=1)

                                    all_result_df.loc[date, f'{topcap_num}_{top_num}_{previous_day}days_cumret'] = \
                                    (1 + top_coin_ret_days_ago).cumprod()[-1] - 1

                    if date > '2017-12-16':
                        for top_num in [1, 3, 5, 10, 50, 100]:
                            if date < '2021-04-13':
                                coins_days_ago = all_result_df.loc[
                                    '2017-12-16', f'{topcap_num}_all_cleaned_coins_name_{kick_small_amount}']
                            else:
                                coins_days_ago = all_result_df.loc[
                                    '2021-04-13', f'{topcap_num}_all_cleaned_coins_name_{kick_small_amount}']

                            if top_num == 1:
                                top_coin_days_ago = coins_days_ago[0:1]
                            elif top_num == 3:
                                top_coin_days_ago = coins_days_ago[1:3]
                            elif top_num == 5:
                                top_coin_days_ago = coins_days_ago[3:5]
                            elif top_num == 10:
                                top_coin_days_ago = coins_days_ago[5:10]
                            elif top_num == 50:
                                top_coin_days_ago = coins_days_ago[10:50]
                            elif top_num == 100:
                                top_coin_days_ago = coins_days_ago[50:100]

                            try:
                                top_coin_drawdown_days_ago = df_price_drawdown.loc[date, top_coin_days_ago].mean()
                            except KeyError:
                                filter_top_coin_days_ago = []
                                for i in top_coin_days_ago:
                                    if i in df_price_drawdown.columns:
                                        filter_top_coin_days_ago.append(i)
                                top_coin_drawdown_days_ago = df_price_drawdown.loc[date, filter_top_coin_days_ago].mean()
                            if isinstance(top_coin_drawdown_days_ago, pd.Series):
                                top_coin_drawdown_days_ago = top_coin_drawdown_days_ago.mean()
                            all_result_df.loc[date, f'{topcap_num}_{top_num}_maxdrawdown'] = top_coin_drawdown_days_ago

        btc_log_price = pd.Series(np.log(df_price['bitcoin']))
        btc_log_price.name = 'log_prices'
        all_df = pd.concat([all_result_df, btc_log_price], axis=1)
        all_df.sort_index(inplace=True)
        all_df = all_df.loc[all_result_df.index[0]:, :]

        all_df.to_csv(result_file_name)
        all_coins_name_df.to_csv(coin_name_file_name)


def analysis_ma_weeks():
    # 获取BTC相关指标数据
    btc_ohlcv_data = pd.read_csv('E:\\data\\ohlcvm_data\\daily\\bitcoin.csv', index_col='end_date')
    btc_df = btc_ohlcv_data[['close']]
    btc_df['log_price'] = np.log10(btc_df['close'])
    for period in [7, 30, 60, 90, 120, 180]:
        btc_df[f'ma_{period}'] = btc_df['close'].rolling(period).mean()
        btc_df[f'btc_osc_{period}'] = (btc_df['close'] - btc_df[f'ma_{period}']) / btc_df[f'ma_{period}']
        # 区间收益率
        btc_df[f'btc_{period}days_ret'] = btc_df['close'].pct_change(period)
        # 高点回撤，低点上涨
        btc_df[f'btc_{period}days_highret'] = (btc_df['close'] - btc_df['close'].rolling(period).max()) / btc_df['close'].rolling(period).max()
        btc_df[f'btc_{period}days_lowret'] = (btc_df['close'] - btc_df['close'].rolling(period).min()) / btc_df['close'].rolling(period).min()
        # 创新高，创新低
        btc_df[f'btc_{period}days_newhigh'] = (btc_df['close'] - btc_df['close'].rolling(period).max())
        btc_df[f'btc_{period}days_newlow'] = (btc_df['close'] - btc_df['close'].rolling(period).min())

    def temp_data_analysis(temp_btc_df=pd.DataFrame(), column_name='', threshold=0, bigger_than=False, smaller_than=False, equal=False):
        temp_series = temp_btc_df[column_name]
        temp_data_analysis = temp_series.copy()
        if equal:
            temp_data_analysis[temp_series == threshold] = 5
            temp_data_analysis[temp_series != threshold] = 0
        if bigger_than:
            temp_data_analysis[temp_series >= threshold] = 5
            temp_data_analysis[temp_series < threshold] = 0
        if smaller_than:
            temp_data_analysis[temp_series <= threshold] = 5
            temp_data_analysis[temp_series > threshold] = 0
        return temp_data_analysis

    for ret_type in ['daily', 'weekly', 'monthly']:
        file_name = os.path.join(WEEKS_ANALYSIS_DATA_DIR, f'All Coins {ret_type} Return Analysis.csv')
        ret_df = pd.read_csv(file_name, index_col='end_date')
        ma_ret_df = pd.DataFrame(ret_df['log_prices'])

        temp_btc_df = btc_df.loc[ma_ret_df.index, :]  # BTC的相关指标

        if ret_type == 'daily':
            ret_gap = [float('-inf'), -1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 5, 10, float('inf')]
        elif ret_type == 'weekly':
            ret_gap = [float('-inf'), -1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 5, 10, float('inf')]
        elif ret_type == 'monthly':
            ret_gap = [float('-inf'), -1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 5, 10, float('inf')]
        else:
            raise NotImplementedError
        ret_gap_list = [[ret_gap[i], ret_gap[i + 1]] for i in range(len(ret_gap) - 1)]

        osc_ret_gap = [float('-inf'), -1, -0.8, -0.6, -0.4, -0.3, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 1, 1.5, 2, 3, 5, 10, float('inf')]
        positive_ret_gap = [0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 1, 1.5, 2, 3, 5, 10, float('inf')]
        negative_ret_gap = [float('-inf'), -1, -0.8, -0.6, -0.5, -0.4, -0.3, -0.2, -0.15, -0.1, -0.05, 0]
        osc_ret_gap_list = [[osc_ret_gap[i], osc_ret_gap[i + 1]] for i in range(len(osc_ret_gap) - 1)]
        positive_ret_gap_list = [[positive_ret_gap[i], positive_ret_gap[i + 1]] for i in range(len(positive_ret_gap) - 1)]
        negative_ret_gap_list = [[negative_ret_gap[i], negative_ret_gap[i + 1]] for i in range(len(negative_ret_gap) - 1)]

        for ma in [7]:  # 以下分析的是不同指标的绝对值与行情指标的关系， 指标数据平滑的窗口可以选择
            ma_ret_df[f'filtered_all_coin_num'] = ret_df[f'all_all_coins_num_True']
            for kick_small_amount in [True]:
                for topcap_num in ['all']:
                    for ma_period in [120]:
                        for gap in osc_ret_gap_list:
                            if f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}' not in ret_df.columns:
                                continue
                            else:
                                ma_ret_df[f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}'] = ret_df[f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}'].rolling(ma).mean()
                        columns_name_list_0 = [f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] >= 0]
                        ma_ret_df[f'{topcap_num}_osc{ma_period}_>0_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_0].sum(axis=1)
                        # BTC上穿下穿时间线
                        ma_ret_df[f'btc_osc{ma_period}_>0'] = temp_data_analysis(temp_btc_df, column_name=f'btc_osc_{ma_period}', threshold=0, bigger_than=True, smaller_than=False, equal=False)
                        columns_name_list_2 = [f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] >= 0.2]
                        ma_ret_df[f'{topcap_num}_osc{ma_period}_>0.2_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_2].sum(axis=1)
                        columns_name_list_4 = [f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] >= 0.4]
                        ma_ret_df[f'{topcap_num}_osc{ma_period}_>0.4_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_4].sum(axis=1)
                        columns_name_list_minus2 = [f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] <= -0.2]
                        ma_ret_df[f'{topcap_num}_osc{ma_period}_<minus0.2_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus2].sum(axis=1)
                        columns_name_list_minus4 = [f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] <= -0.4]
                        ma_ret_df[f'{topcap_num}_osc{ma_period}_<minus0.4_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus4].sum(axis=1)
                        all_columns = [f'{topcap_num}_osc{ma_period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list]
                        ma_ret_df.drop(columns=all_columns, inplace=True)

                    # 每日日收益率，周收益率，月收益分布数据平滑
                    for gap in ret_gap_list:
                        ma_ret_df[f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}'] = ret_df[f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}'].rolling(ma).mean()
                    columns_name_list_0 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] > 0]
                    ma_ret_df[f'{topcap_num}_>0_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_0].sum(axis=1)
                    columns_name_list_1 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] > 0.1]
                    ma_ret_df[f'{topcap_num}_>0.1_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_1].sum(axis=1)
                    columns_name_list_2 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] > 0.2]
                    ma_ret_df[f'{topcap_num}_>0.2_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_2].sum(axis=1)
                    columns_name_list_4 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] > 0.4]
                    ma_ret_df[f'{topcap_num}_>0.4_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_4].sum(axis=1)
                    columns_name_list_5 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] > 0.5]
                    ma_ret_df[f'{topcap_num}_>0.5_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_5].sum(axis=1)
                    columns_name_list_6 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] > 1]
                    ma_ret_df[f'{topcap_num}_>1_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_6].sum(axis=1)
                    columns_name_list_7 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] > 3]
                    ma_ret_df[f'{topcap_num}_>3_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_7].sum(axis=1)
                    columns_name_list_8 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] > 5]
                    ma_ret_df[f'{topcap_num}_>5_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_8].sum(axis=1)
                    columns_name_list_minus1 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] <= -0.1]
                    ma_ret_df[f'{topcap_num}_<minus0.1_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus1].sum(axis=1)
                    columns_name_list_minus2 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] <= -0.2]
                    ma_ret_df[f'{topcap_num}_<minus0.2_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus2].sum(axis=1)
                    columns_name_list_minus4 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] <= -0.4]
                    ma_ret_df[f'{topcap_num}_<minus0.4_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus4].sum(axis=1)
                    columns_name_list_minus5 = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list if gap[1] <= -0.5]
                    ma_ret_df[f'{topcap_num}_<minus0.5_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus5].sum(axis=1)
                    all_columns = [f'{topcap_num}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in ret_gap_list]
                    ma_ret_df.drop(columns=all_columns, inplace=True)

                    # 统计每天创新高创新低分布情况
                    if ret_type == 'daily':
                        # for highlow_period in [30, 60, 90, 120, 180]:
                        for highlow_period in [90]:
                            ma_ret_df[f'all_high{highlow_period}_num_ratio_smalltrade_True'] = ret_df[f'all_high{highlow_period}_num_ratio_smalltrade_True']
                            ma_ret_df[f'all_low{highlow_period}_num_ratio_smalltrade_True'] = ret_df[f'all_low{highlow_period}_num_ratio_smalltrade_True']

                    # 统计全市场横截面波动率
                    ma_ret_df[f'whole_market_ret_divergence_True'] = ret_df[f'whole_market_ret_divergence_True']

                    # 统计每天1,3, 4, 6月绝对收益率分布情况（均线处理）
                    if ret_type == 'daily':
                        for period in [30, 90, 120, 180]:
                            for gap in osc_ret_gap_list:
                                ma_ret_df[f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}'] = ret_df[f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}'].rolling(ma).mean()
                            columns_name_list_1 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] > 0.1]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}>0.1_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_1].sum(axis=1)
                            columns_name_list_2 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] > 0.2]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}>0.2_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_2].sum(axis=1)
                            columns_name_list_4 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] > 0.4]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}>0.4_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_4].sum(axis=1)
                            columns_name_list_5 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] > 0.5]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}>0.5_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_5].sum(axis=1)
                            columns_name_list_6 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] > 1]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}>1_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_6].sum(axis=1)
                            columns_name_list_7 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] > 3]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}>3_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_7].sum(axis=1)
                            columns_name_list_8 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] > 5]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}>5_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_8].sum(axis=1)
                            columns_name_list_minus1 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] <= -0.1]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}<minus0.1_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus1].sum(axis=1)
                            columns_name_list_minus2 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] <= -0.2]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}<minus0.2_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus2].sum(axis=1)
                            columns_name_list_minus4 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] <= -0.4]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}<minus0.4_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus4].sum(axis=1)
                            columns_name_list_minus5 = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list if gap[1] <= -0.5]
                            ma_ret_df[f'{topcap_num}_monthlyret{period}<minus0.5_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus5].sum(axis=1)
                            all_columns = [f'{topcap_num}_monthlyret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in osc_ret_gap_list]
                            ma_ret_df.drop(columns=all_columns, inplace=True)


                    # 统计与BTC相比，近1天，近一周，近1,3,6月相对收益率分布情况（均线处理）
                    if ret_type == 'daily':
                        for compare_period in ['daily', 'weekly', '1monthly', '3monthly', '6monthly']:
                            if compare_period == 'daily':
                                threshold_list = [0, 0.001, 0.005, 0.01, 0.03, 0.05, 0.1]
                            elif compare_period == 'weekly':
                                threshold_list = [0, 0.01, 0.03, 0.05, 0.1, 0.15, 0.2]
                            elif compare_period == '1monthly':
                                threshold_list = [0, 0.05, 0.1, 0.15, 0.2, 0.3]
                            elif compare_period == '3monthly':
                                threshold_list = [0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.5]
                            elif compare_period == '6monthly':
                                threshold_list = [0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.5]
                            else:
                                raise NotImplementedError
                            for threshold in threshold_list:
                                ma_ret_df[f'all_{compare_period}_ret_biggerthan_btc_{threshold}_ratio_smalltrade_{kick_small_amount}_ma{ma}'] = ret_df[f'all_{compare_period}_ret_biggerthan_btc_{threshold}_ratio_smalltrade_{kick_small_amount}'].rolling(ma).mean()
                            for threshold in threshold_list:
                                ma_ret_df[f'all_{compare_period}_ret_smallerthan_btc_{-threshold}_ratio_smalltrade_{kick_small_amount}_ma{ma}'] = ret_df[f'all_{compare_period}_ret_smallerthan_btc_{-threshold}_ratio_smalltrade_{kick_small_amount}'].rolling(ma).mean()


                    # 统计每天近1周，近1,3,4,6月高点回撤，低点上涨收益率分布情况（均线处理）
                    if ret_type == 'daily':
                        for period in [7, 30, 90, 120, 180]:
                        # for period in [90]:
                            # 统计高点回撤收益率分布
                            for gap in negative_ret_gap_list:
                                ma_ret_df[f'{topcap_num}_highret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}'] = ret_df[f'{topcap_num}_highret{period}_{gap}_smalltrade_{kick_small_amount}'].rolling(ma).mean()
                            columns_name_list_minus1 = [f'{topcap_num}_highret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in negative_ret_gap_list if gap[1] <= -0.1]
                            ma_ret_df[f'{topcap_num}_highret{period}<minus0.1_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus1].sum(axis=1)
                            columns_name_list_minus2 = [f'{topcap_num}_highret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in negative_ret_gap_list if gap[1] <= -0.2]
                            ma_ret_df[f'{topcap_num}_highret{period}<minus0.2_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus2].sum(axis=1)
                            columns_name_list_minus4 = [f'{topcap_num}_highret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in negative_ret_gap_list if gap[1] <= -0.4]
                            ma_ret_df[f'{topcap_num}_highret{period}<minus0.4_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus4].sum(axis=1)
                            columns_name_list_minus5 = [f'{topcap_num}_highret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in negative_ret_gap_list if gap[1] <= -0.5]
                            ma_ret_df[f'{topcap_num}_highret{period}<minus0.5_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_minus5].sum(axis=1)
                            all_columns = [f'{topcap_num}_highret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in negative_ret_gap_list]
                            ma_ret_df.drop(columns=all_columns, inplace=True)
                            # 统计低点回升收益率分布
                            for gap in positive_ret_gap_list:
                                ma_ret_df[f'{topcap_num}_lowret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}'] = ret_df[f'{topcap_num}_lowret{period}_{gap}_smalltrade_{kick_small_amount}'].rolling(ma).mean()
                            columns_name_list_1 = [f'{topcap_num}_lowret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in positive_ret_gap_list if gap[1] > 0.1]
                            ma_ret_df[f'{topcap_num}_lowret{period}>0.1_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_1].sum(axis=1)
                            columns_name_list_2 = [f'{topcap_num}_lowret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in positive_ret_gap_list if gap[1] > 0.2]
                            ma_ret_df[f'{topcap_num}_lowret{period}>0.2_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_2].sum(axis=1)
                            columns_name_list_4 = [f'{topcap_num}_lowret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in positive_ret_gap_list if gap[1] > 0.4]
                            ma_ret_df[f'{topcap_num}_lowret{period}>0.4_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_4].sum(axis=1)
                            columns_name_list_5 = [f'{topcap_num}_lowret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in positive_ret_gap_list if gap[1] > 0.5]
                            ma_ret_df[f'{topcap_num}_lowret{period}>0.5_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_5].sum(axis=1)
                            columns_name_list_6 = [f'{topcap_num}_lowret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in positive_ret_gap_list if gap[1] > 1]
                            ma_ret_df[f'{topcap_num}_lowret{period}>1_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_6].sum(axis=1)
                            columns_name_list_7 = [f'{topcap_num}_lowret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in positive_ret_gap_list if gap[1] > 3]
                            ma_ret_df[f'{topcap_num}_lowret{period}>3_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_7].sum(axis=1)
                            columns_name_list_8 = [f'{topcap_num}_lowret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in positive_ret_gap_list if gap[1] > 5]
                            ma_ret_df[f'{topcap_num}_lowret{period}>5_smalltrade_{kick_small_amount}_ma{ma}'] = ma_ret_df[columns_name_list_8].sum(axis=1)
                            all_columns = [f'{topcap_num}_lowret{period}_{gap}_smalltrade_{kick_small_amount}_ma{ma}' for gap in positive_ret_gap_list]
                            ma_ret_df.drop(columns=all_columns, inplace=True)

                    # 统计与BTC相比，每天近1周，近1,3,4,6月高点回撤，低点上涨相对收益率分布情况（均线处理）
                    if ret_type == 'daily':
                        for compare_period in [7, 30, 90, 120, 180]:
                            if compare_period == 7:
                                highret_threshold_list = [0.01, 0.03, 0.05, 0.1, 0.15, 0.2]
                                lowret_threshold_list = [0.01, 0.03, 0.05, 0.1, 0.15, 0.2]
                            elif compare_period == 30:
                                highret_threshold_list = [0.03, 0.05, 0.1, 0.15, 0.2, 0.3]
                                lowret_threshold_list = [0.03, 0.05, 0.1, 0.15, 0.2, 0.3]
                            elif compare_period == 90:
                                highret_threshold_list = [0.03, 0.05, 0.1, 0.15, 0.2, 0.3]
                                lowret_threshold_list = [0.05, 0.1, 0.15, 0.2, 0.3, 0.5]
                            elif compare_period == 120:
                                highret_threshold_list = [0.03, 0.05, 0.1, 0.15, 0.2, 0.3]
                                lowret_threshold_list = [0.05, 0.1, 0.15, 0.2, 0.3, 0.5]
                            elif compare_period == 180:
                                highret_threshold_list = [0.03, 0.05, 0.1, 0.15, 0.2, 0.3]
                                lowret_threshold_list = [0.05, 0.1, 0.15, 0.2, 0.3, 0.5]
                            else:
                                raise NotImplementedError

                            for threshold in highret_threshold_list:
                                ma_ret_df[f'all_{compare_period}_highret_smallerthan_btc_{-threshold}_ratio_smalltrade_{kick_small_amount}_ma{ma}'] = ret_df[f'all_{compare_period}_highret_smallerthan_btc_{-threshold}_ratio_smalltrade_{kick_small_amount}'].rolling(ma).mean()
                            for threshold in lowret_threshold_list:
                                ma_ret_df[f'all_{compare_period}_lowret_biggerthan_btc_{threshold}_ratio_smalltrade_{kick_small_amount}_ma{ma}'] = ret_df[f'all_{compare_period}_lowret_biggerthan_btc_{threshold}_ratio_smalltrade_{kick_small_amount}'].rolling(ma).mean()

                    # 50 in 50 数据（均线处理）
                    for previous_day in [180]:
                        for top_num in [50]:
                            # 均线前数据
                            ma_ret_df[f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago'] = ret_df[f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago']
                            ma_ret_df[f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago_ratio'] = ret_df[f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago_ratio']
                            ma_ret_df[f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago_ma{ma}'] = ret_df[f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago'].rolling(ma).mean()
                            ma_ret_df[f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago_ratio_ma{ma}'] = ret_df[f'{topcap_num}_{top_num}_in_{top_num}_{previous_day}daysago_ratio'].rolling(ma).mean()

                    # 不同市值组别不同时间区间的累计收益率差异, 不同市值组别的最大回撤数据（均线处理）
                    if ret_type == 'daily':
                        # for previous_day in [30, 90, 180, 365]:
                        for previous_day in [90]:
                            for top_num in [1, 3, 5, 10, 50, 100]:
                                ma_ret_df[f'{topcap_num}_{top_num}_{previous_day}days_cumret'] = ret_df[f'{topcap_num}_{top_num}_{previous_day}days_cumret']  # 均线前数据
                                ma_ret_df[f'{topcap_num}_{top_num}_{previous_day}days_cumret'][ma_ret_df[f'{topcap_num}_{top_num}_{previous_day}days_cumret'] > 0] = 0

                        for top_num in [1, 3, 5, 10, 50, 100]:
                            ma_ret_df[f'{topcap_num}_{top_num}_maxdrawdown'] = ret_df[f'{topcap_num}_{top_num}_maxdrawdown']  # 均线前数据
                            ma_ret_df[f'{topcap_num}_{top_num}_maxdrawdown_ma{ma}'] = ret_df[f'{topcap_num}_{top_num}_maxdrawdown'].rolling(ma).mean()


        file_name = os.path.join(WEEKS_ANALYSIS_DATA_DIR, f'All Coins {ret_type} Return MA Analysis.csv')
        ma_ret_df = ma_ret_df.loc['2017-01-01':, ]
        ma_ret_df.to_csv(file_name)


if __name__ == '__main__':
    analysis_weeks()
    analysis_ma_weeks()
